Repository navigation
fix(sessions): reconcile forked-session fold lineage from disk at boot (#2807) - #2887
Conversation
ranxianglei#2807) A forked child session starts with an empty fold ledger while carrying the parent's full raw history, so preflight re-estimates and re-folds everything and the payload overruns the model window. The lineage is written to metadata.derivedFromSessionId but never read back, so a restart (or a fork that missed the byte-exact prefix match) left the child orphaned. Add reconcileDerivedLineage(), run after initSessions()/loadConversations(): for every session that records a parent, holds no blocks, and has not yet adopted, load the parent from disk and re-run planForkAdoption + applyForkAdoption by message-id intersection — copy-on-fork, parent untouched, and the CCR content store is cloned for the inherited refs. Also fix the weak derived path: it read opts.forkAdoption (default OFF), so it never adopted blocks while the boot reconcile still did — two inheritance paths answering to two different switches. Gate it on resumeInheritance, the same switch as the strong resume path, and move the derivedBlocksAdopted marker to after the adoption call so a partial overlap does not close the gate permanently. Tests: tests/lineage-reconcile-boot.test.ts (adoption, idempotency across boots, resumeInheritance:false) and tests/derived-fork-block-inheritance.test.ts (byte-differing fork inherits, non-overlapping ids inherit nothing).
2074cbc to
ac29ecd
Compare
… review) reconcileDerivedLineage resolves parents via peekSession/getStore().loadSync (session id), never through the plugin conversation routing table — the real ordering constraint is initSessions(), not loadConversations().
[bot] 🏷 Review 完成 — 暂缓合并(缺回归测试),方向与机制认可。 已拉取分支、rebase 到当前 master(干净)、本地 typecheck + 定向测试全绿、逐行审完 3 个文件(+148/-1)。结论按八问如下。 结构化八问
拦点(阻塞合并)PR 描述的测试证据在分支里不存在。 描述声称 green 的 顺手修掉(已在分支上补 commit
|
…s adoption gates (ranxianglei#2807) The PR described tests/lineage-reconcile-boot.test.ts and tests/derived-fork-block-inheritance.test.ts as green, but only the src files had been staged — the tests were untracked, so the branch carried no regression coverage for reconcileDerivedLineage at all. Both files are added now: - lineage-reconcile-boot: AC9 boot adoption, AC10 cross-boot idempotence plus an unreachable-parent orphan check, AC11 resumeInheritance:false, AC12 multi-hop lineage gate deferral. - derived-fork-block-inheritance: byte-prefix fork inheritance and the no-intersection case. AC12 is the regression test for review finding 2: reconcileDerivedLineage walks sessions by lastSeen descending, so in a P->C->G chain the grandchild is reached before the child has adopted from its own parent. Three branches (parent has no blocks, child has no refs, nothing fully present yet) previously set derivedBlocksAdopted, closing the gate permanently; the grandchild could then only rely on preflight compression. They now leave the marker unset so the next boot retries, matching the existing 'parent not on disk' philosophy. AC12 fails without this change and passes with it. Also corrects the boot comment: the sweep resolves parents by session id (peekSession, then the store), so the real ordering constraint is initSessions(), not loadConversations() — the plugin routing table is never consulted.
c972b2d to
7831c90
Compare
|
[bot] 🏷 Verified against current master (2026-10-13):
Ready for owner review/merge. |
|
Review 收尾:拦点清零 + update-branch + 维护者重构(行为不变) ① 原 review 硬拦点已清(核实过分支现状)
② Finding 2(多跳过早关门)已按 review 建议实现 ③ update-branch:已并入最新 master( ④ 维护者重构提交 ⑤ owner 结论
描述勘误:原文 "metadata keys pre-exist, nothing new" 不准确—— |
[bot] 🏷 复核完成:五条全部核实属实,拦点清零。 本轮拉取当前 head( ① 测试文件 — 存在且断言有牙
② Finding 2(多跳过早关门)— 按建议实现且更完整 ③ update-branch — 确认 ④ 重构 本地定向验证(本轮): 结构化八问(收口轮)
判定:可合并。 等最后两个 windows test job 转绿即可;合并按仓库规矩由人工执行。残余限制(进程内不主动重试、非成功分支要等下次 boot)与 ref-number drift 边界风险维持我第一轮评估:已知、有界、接受。 |
|
CI note: the earlier red on
No code change needed; head |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.368-master.48c281b # this exact build
npm install -g billion-context@master # always the newest merged build
|
Issues
Closes #2985 — fork child sessions orphaned across a bili restart:
metadata.derivedFromSessionIdis persisted but no code path ever reads it back to trigger block adoption, so the child serves with zero blocks and re-sends the full shared prefix until the upstream rejects the over-window payload. (Family origin #2807 is already closed; this PR covers the cross-restart facet it left behind.)Summary
Three breaks of the same root cause — "lineage and ledger live only in process
memory" — are now closed from the disk side:
resume-inheritanceneeds a byte-exact prefix chainpendingRegisters)derivedParentis undefined and the weak gate never opensmetadata.derivedFromSessionIdwas written but never read backreconcileDerivedLineageaddresses ③ (and with it ②: no register table needed);the weak path gains real block adoption for ①'s fork variant.
Second fix in the same change: the weak
derivedpath readopts.forkAdoption(default OFF) while the boot reconcile read
opts.resumeInheritance(default ON) — two inheritance paths answering to two different switches,
so the weak path never adopted at all. It is now gated on
resumeInheritancelike the strong resume path (#1834/#629 reserve
forkAdoptionfor anonymousforks), and the
derivedBlocksAdoptedmarker moves to after the adoptioncall so a partial id overlap cannot close the gate permanently.
Evidence
Real session
01a12de8(fork child, 34 compressible ranges fully exposed,state.blocks: [],derivedFromSessionIdintact on disk):Before — first request after restart:
After — same session, one boot later:
Disk:
state.blocks0 → 3,blockContents0 → 3,messageRefs.byRaw1580,stats.lastInputTokens0 → 35908. This file was never hand-edited — therecovery is code-only.
Tests (
node --import tsx --test):npx tsc --noEmitclean. Full suite: 1788 pass, and the failures(
installer lane,cache-friendly*,cache proof,e2e chat relay) reproduceon unmodified
master— verified by stashing the diff and re-running.Not covered:
R5from the originating PRD (estimateCoreMessagesUppercountscharacters as tokens on the
unknownBaselinepath) is deliberately out ofscope here — fixing ③ means
lastInputTokensis no longer 0, so that pathstops firing. Flagging it as a separate issue.
Agent verification (2026-10-13, platform daemon)
Verified on head
7831c9013trial-merged onto current masterc60c2d109(40 commits ahead of the branch base): clean auto-merge, zero conflicts;tsc --noEmitclean.tests/lineage-reconcile-boot.test.ts4/4 (AC9–AC12),tests/derived-fork-block-inheritance.test.ts2/2. (The counts/names in the block above predate commit7831c9013, which added AC12 and reworked the second test.)src/fork-adoption.ts/src/server.ts/src/server/handle.tsmakes 4 of the 6 tests fail (AC9, AC10, AC12, id-intersection adoption); AC4 and AC11 pass on master by design (correct no-overlap behaviour / gate-off trivially holds).fork-adoption*,fork-ccr-adoption,issue1333-derived-inheritance,dsh-fork-adopt,opencode-fork-adopt,pi-omp-fork-adopt,plugin-fork,resume-adoption-wires,resume-ref-inheritance,truncation-fork,claude-fork-lineage,claude-transcript-lineage,issue2601-persona-fork-lineage,fork-nudge-trigger,fork-side-request-lane,preflight-fork-estimate) — 163/163 pass.Behavior Changes (old → new)
Intentional semantics shifts introduced by this PR (disclosure per repo policy):
reconcileDerivedLineage) for children that already had requests before the restart. Why: the ledger was persisted but no code path ever readderivedFromSessionIdback to trigger adoption.resumeInheritancescope widens (default ON, value unchanged). Old: gated only the strong resume path (Claude 上 resume 导致消息错乱 #1486); the weak derived path answered toforkAdoption(default OFF) and therefore never adopted. New: gates both the weak-path adoption and the boot reconcile. The existing opt-out (resumeInheritance: false) keeps working and now also disables the boot sweep.metadata.derivedBlocksAdopted(boolean). Set only after a successful adoption; deliberately NOT set on any non-success case, so multi-hop chains (P→C→G where the intermediate parent is still empty at sweep time) keep retrying on later boots. Purely additive: older readers ignore the unknown key.Merge Danger
Door: two-way — all three files are additive; reverting the commit restores
masterbehaviour. No schema migration: exactly one new purely-additivemetadatakey (derivedBlocksAdopted, boolean) is introduced — older readers ignore unknown keys — and nothing migrates or deletes state.Blast Radius: session boot path.
reconcileDerivedLineageruns once perboot over every session on disk, so the cost scales with session-file count
(it is gated to sessions that record a parent, hold no blocks, and have not
adopted). Default behaviour is unchanged when
resumeInheritance: false.